package com.example.nacosdemo.nacosdiscovery;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/api/discovery")
public class DiscoveryController {

    @NacosInjected
    private NamingService namingService;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "get",method = RequestMethod.GET)
    public List<Instance> get(@RequestParam String serviceName) throws NacosException{
        return namingService.getAllInstances(serviceName);
    }

    @RequestMapping(value = "get/config",method = RequestMethod.GET)
    public String get1() throws NacosException{
        String url =
                "http://"+namingService.getAllInstances("example").get(0).getIp()+":"+namingService.getAllInstances("example").get(0).getPort()+"/api/v1/config/cloud/get/value/111/";
        return restTemplate.getForObject(url,String.class);
    }
}
